package com.boot.down.async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.Future;

@Component
public class AsyncTask {

    private Random random = new Random();

    @Async
    public Future<String> doTaskOne() throws Exception{

        System.out.println("任务一开始");
        long start  = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("任务一消耗时间：" + (end - start));
        return new AsyncResult<>("任务一完成");
    }

    @Async
    public Future<String> doTaskTwo() throws Exception{
        System.out.println("任务二开始");
        long start1  = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end1 = System.currentTimeMillis();
        System.out.println("任务二消耗时间：" + (end1 - start1));
        return new AsyncResult<>("任务二完成");
    }

    @Async
    public Future<String> doTaskThree() throws Exception{
        System.out.println("任务三开始");
        long start3  = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end3 = System.currentTimeMillis();
        System.out.println("任务三消耗时间：" + (end3 - start3));
        return new AsyncResult<>("任务三完成");
    }

}
